package com.example.no1admin.controller;


import com.example.no1admin.pojo.dto.store.StoreAddNewDTO;
import com.example.no1admin.pojo.dto.store.StoreEditDTO;
import com.example.no1admin.pojo.vo.store.StoreListItemVO;
import com.example.no1admin.service.IStoreService;
import com.example.no1admin.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "门店管理模块")
@RequestMapping("/stores")
@Slf4j
@RestController
public class StoreController {

    @Autowired
    IStoreService storeService;

    @ApiOperation("创建门店")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody StoreAddNewDTO storeAddNewDTO){
        log.debug("接受到的请求参数:{}",storeAddNewDTO);
        storeService.addNew(storeAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除门店")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult deleteById(@PathVariable Long id){
        log.debug("接收到删除门店的请求");
        storeService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改门店")
    @PreAuthorize("hasAuthority('ROLE_admin')")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult updateById(StoreEditDTO storeEditDTO){
        log.debug("接收到修改门店的请求");
        storeService.updateById(storeEditDTO);
        return JsonResult.ok();
    }

    @ApiOperation("门店列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult list(){
        log.debug("接收到查询门店列表的请求");
        List<StoreListItemVO> stores = storeService.list();
        return JsonResult.ok(stores);
    }
}
